package com.example.a1.fannong.core.manager.version;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;

import com.example.a1.fannong.core.app.BaseManager;
import com.example.a1.fannong.core.app.MyApplication;
import com.example.a1.fannong.core.app.PathConfig;
import com.example.a1.fannong.core.http.FileDownloader;
import com.example.a1.fannong.core.http.OnResultListener;
import com.example.a1.fannong.model.VersionInfo;

import java.io.File;


/**
 * Created by nishuhang on 17/7/5.
 */

public class VersionManager extends BaseManager {
    private VersionInfo versionInfo;
    private static final String APK_NAME = "smartConsumer_";

    @Override
    public void onManagerCreate(MyApplication application) {

    }


    public void checkUpdate(final OnResultListener<VersionInfo> onResultListener) {
//        GetAppVersionInfoRequester requester = new GetAppVersionInfoRequester(new OnResultListener<VersionInfo>() {
//            @Override
//            public void onResult(int result, String msg, VersionInfo versionInfo) {
//                if (result == RESULT_SUCCESS) {
//                    float nowVersion = Float.valueOf(BuildConfig.VERSION_NAME);
//                    if (versionInfo.getVersionCount() > nowVersion) {
//                        onResultListener.onResult(RESULT_SUCCESS, "", versionInfo);
//                    } else {
//                        onResultListener.onResult(RESULT_SUCCESS, "", null);
//                    }
//                } else {
//                    onResultListener.onResult(RESULT_FAILED, "", null);
//                }
//
//            }
//        });
//        requester.doGet();

    }

    public void downLoadApk(final Context context, VersionInfo versionInfo, final FileDownloader.DownloadListener downloadListener) {
        final File localPathFile = new File(PathConfig.getTempFile(), "smartConsumer_" + versionInfo.getVersionCount() + ".apk");
        if (localPathFile.exists()) {
            localPathFile.delete();
        }
        FileDownloader fileDownloader = new FileDownloader(localPathFile.getAbsolutePath(), versionInfo.getApkUrl(), new FileDownloader.DownloadListener() {
            @Override
            public void onProgressChange(String filePath, int totalSize, int currentSize) {
                downloadListener.onProgressChange(filePath, totalSize, currentSize);
            }

            @Override
            public void onDownloadFinish(String filePath, FileDownloader.DownloadResult downloadResult) {
                downloadListener.onDownloadFinish(filePath, downloadResult);
                openFile(context, localPathFile.getAbsolutePath());
            }
        });
        new Thread(fileDownloader).start();

    }

    /**
     * 打开下载的apk
     */
    public void openFile(Context context, String filePath) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        File file = new File(filePath);
        if (Build.VERSION.SDK_INT >= 24) {
            Uri contentUri = FileProvider.getUriForFile(context, "cn.personal.smartsupervise" + ".fileProvider", file);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
        context.startActivity(intent);
    }
}
